﻿/// <summary>
/// FileName: ConstantExpression.cs
/// Author: Jiang Xiaolong
/// Created Time: 2015.05.12
/// Version: 1.0
/// Company: Sunnytech
/// Function: 函数表达式；
///
/// Changed By:
/// Modification Time:
/// Discription:
/// </summary>
using System;
using System.Collections.Generic;
using Expression.api;

namespace Expression.impl
{
	public class FunctionExpression : ExpressionTree
	{
		public Type ExpType
		{
			get { return _function.ExpType; }
			set { }
		}

		public ICustomFunction _function;
		public List<ExpressionTree> _parameters = new List<ExpressionTree>();

		public object Evaluate()
		{
			return _function.Evaluate(_parameters);
		}

		public void SetFunction(ICustomFunction function)
		{
			this._function = function;
		}

		public void AddParameter(ExpressionTree exp)
		{
			_parameters.Add(exp);
		}

		public int ParameterCount()
		{
			return _parameters.Count;
		}

		public override string ToString()
		{
			int functionIndex = _function.ToString().LastIndexOf('.');
			string printStr = _function.ToString().Substring(functionIndex + 1) + "( ";
			for (int i = 0; i < ParameterCount(); i++)
			{
				if (i > 0)
				{
					printStr += ", " + _parameters[i].ToString();
				}
				else
				{
					printStr += _parameters[i].ToString();
				}
			}
			return printStr + " )";
		}

        public ExpressionTree Clone ()
        {
            FunctionExpression expClone = new FunctionExpression();
            expClone._function = this._function;
            for (int i = 0; i < this._parameters.Count; i++)
            {
                expClone._parameters.Add(this._parameters[i].Clone());
            }
            return expClone;
        }
	}
}


